<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/1/29
 * Time: 17:26
 */

namespace app\common\cachemodel\zl7;

use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Zl7AiPredictionActive extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl7_ai_prediction_active';

    protected $cacheModel = 'ai_answer.ai_prediction';

    /**
     * 添加数据
     * @param $uid
     * @param $data
     * @return int|string
     */
    public function saveData($uid, $data)
    {
        try
        {
            $id = $this->insertGetId($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setCacheKeyDetail('last_question', $uid)->rmCache('last_question');
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
        return $id;
    }

    public function updateData($id, $data)
    {
        try
        {
            $this->where('id', $id)->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['uid'])->setCacheKeyDetail('last_question', $data['uid'])->rmCache('last_question');
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 获取用户最新的提问
     * @param $uid
     * @return array|bool|mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserLastQuestion($uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('last_question', $uid)->setCacheTime(600)->getDataAndSet(function () use ($uid)
        {
            try
            {
                return $this->where('uid', $uid)->order('id', 'DESC')->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                return [];
            }
        }, [], 'last_question');
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $data = $this->where(['id' => ['in', $ids]])->column('*', 'id');
            } else
            {
                $data = $this->limit(10)->column('*', 'id');
            }
        }
        catch (\Exception $e)
        {
            Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            $data = [];
        }
        return $data;
    }
}
